package com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.activity.GameChoiceListViewSecondActivity;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceGridViewInfo;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 郎志奇 on 2016/12/27.
 */

public class GameChoiceGridViewAdapter extends BaseAdapter {


    private List<GameChoiceGridViewInfo> data;

    private Context context;

    public GameChoiceGridViewAdapter(List<GameChoiceGridViewInfo> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        Log.e("TAG", "getCount: " + data.size());
        return data != null ? data.size() : 0;


    }

    @Override
    public GameChoiceGridViewInfo getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.item_fragment_game_choice_gridview_item, parent, false);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        //设置数据

        holder.mItemFragmentGameChoiceGridViewItemTextName.setText(
                data.get(position).getName()
        );

        holder.mItemFragmentGameChoiceGridViewItemTextAppsize.setText(
                Integer.parseInt(data.get(position).getPackage_size()) / (1024 * 1024) + "MB"
        );

        Picasso.with(context).load(Contacts.IMAGE_PREFIX + data.get(position).getIcon_url())
                .into(holder.mItemFragmentGameChoiceGridViewItemImage);

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(context, GameChoiceListViewSecondActivity.class);

                intent.putExtra("id",data.get(position).getId());

                context.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {

        @InjectView(R.id.item_fragment_game_choice_gridView_item_linearLayout)
        LinearLayout linearLayout;
        @InjectView(R.id.item_fragment_game_choice_gridView_item_image)
        ImageView mItemFragmentGameChoiceGridViewItemImage;
        @InjectView(R.id.item_fragment_game_choice_gridView_item_text_name)
        TextView mItemFragmentGameChoiceGridViewItemTextName;
        @InjectView(R.id.item_fragment_game_choice_gridView_item_text_appsize)
        TextView mItemFragmentGameChoiceGridViewItemTextAppsize;
        @InjectView(R.id.item_fragment_game_choice_gridView_item_button_download)
        Button mItemFragmentGameChoiceGridViewItemButtonDownload;

        ViewHolder(View convertView) {
            ButterKnife.inject(this, convertView);
        }
    }
}
